package com.example.service.Impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.CustomException;
import com.example.mapper.ProjectMapper;
import com.example.mapper.TeamAndUserMapper;
import com.example.mapper.TeamMapper;
import com.example.pojo.entity.Project;
import com.example.pojo.entity.Team;
import com.example.pojo.entity.TeamAndUser;
import com.example.service.AdminService;
import com.example.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告信息表业务处理
 **/
@Service
@DS("master")
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private @Lazy AdminService adminService;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TeamAndUserMapper teamAndUserMapper;

    /**
     * 新增
     */
    public void add(Team team) {
        Team one = this.selectByTeamName(team.getName());
        if (one != null){
            throw new CustomException(ResultCodeEnum.TEAM_NAME_ERROR);
        }
        team.setNum(0);
        team.setIsDeleted(0);
        team.setVersion(0);
        team.setSetTime(LocalDate.now());
        teamMapper.insert(team);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        Team team = selectById(id);
        team.setIsDeleted(1);
        teamMapper.updateById(team);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            Team team = selectById(id);
            team.setIsDeleted(1);
            teamMapper.updateById(team);
        }
    }

    /**
     * 根据ID查询
     */
    public Team selectById(Integer id) {
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询
     */
    public Page<Team> selectPage(String name, String projectName, String order, Integer pageNum, Integer pageSize) {
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        if (projectName != null){
            QueryWrapper<Project> wrapper = new QueryWrapper<>();
            wrapper.like("name",projectName);
            Project project = projectMapper.selectOne(wrapper);
            queryWrapper.eq("project_id",project.getId());
        }
        if (order != null){
            if (order.equals("ascend")){
                queryWrapper.orderByAsc("num");
            }
            if (order.equals("descend")){
                queryWrapper.orderByDesc("num");
            }
        }
        Page<Team> page = teamMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<Team> records = page.getRecords();
        for (Team record : records) {
            if (record.getProjectId() != null){
                record.setProjectName(projectMapper.selectById(record.getProjectId()).getName());
            }
//            record.setNum(teamAndUserServiceImpl.getCountByTeamId(record.getId()));
        }
        return page;
    }

    /**
     * 根据团队名称获取队伍信息
     */
    public Team selectByTeamName(String teamName) {
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(teamName),"name",teamName);
        queryWrapper.eq("is_deleted",0);
        return getOne(queryWrapper);
    }

    /**
     * 根据团队id获取团队人数列表
     */
    public List<Integer> selectUserById(Integer id) {
        List<TeamAndUser> teamAndUsers = teamAndUserMapper.selectList(new LambdaQueryWrapper<TeamAndUser>().eq(TeamAndUser::getTeamId, id));
        List<Integer> userIdList = teamAndUsers.stream().map(TeamAndUser::getUserId).collect(Collectors.toList());

        return userIdList;
    }

    /**
     * 将成员从团队中删去
     */
    public void deleteUser(Integer id, Integer teamId) {
        teamAndUserMapper.delete(new LambdaQueryWrapper<TeamAndUser>()
                .eq(TeamAndUser::getUserId, id)
                .eq(TeamAndUser::getTeamId, teamId));
    }
}
